<template>
  <!-- 采购入库 -->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter='24'>
          <a-col :md='6' :sm='6'>
            <a-form-item label='采购入库单号'>
              <a-input v-model='queryParam.serial' placeholder='请输入采购入库单号' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='供应商'>
              <j-search-select-tag v-model='queryParam.supplierId' dict='hf_supplier,name,id'
                                   placeholder='请选择供应商' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='供应商订单号'>
              <a-input v-model='queryParam.supplierOrderSerial' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='关联采购订单号'>
              <a-input v-model='queryParam.sourceSerial' type='list' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='单据日期'>
              <j-date v-model='queryParam.documentDate' style='width: 100%' type='list' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='日期区间'>
              <a-range-picker v-model='queryParam.dateInterval' type='list' @change='handleDateInterval' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='物料代码'>
              <a-input v-model='queryParam.materialCode' style='width: 100%' type='list' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='物料名称'>
              <a-input v-model='queryParam.materialName' style='width: 100%' type='list' />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('采购入库单')">导出</a-button>
      <a-upload
        :action="importExcelUrl"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <a-button
        style="background: #FF8F00;border: #FF8F00;"
        icon="import"
        type="primary"
        @click="uploadModalJD.visible = true"
        >金蝶导入</a-button
      >
      <a-modal
        :visible="uploadModalJD.visible"
        @cancel="
          () => {
            uploadModalJD.visible = false
          }
        "
      >
        <a-spin :spinning="uploadModalJD.loading">
          <a-row :gutter="[16, 16]">
            <a-col :span="24">
              <a-upload :multiple="false" :showUploadList="false" name="file" :customRequest="customUpload">
                <a-button icon="import" type="primary">选择金蝶导出的文件</a-button>
              </a-upload>
            </a-col>
          </a-row>
        </a-spin>
      </a-modal>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot='isClosed' slot-scope='text'>
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot='status' slot-scope='text'>
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot='action' slot-scope='text, record'>
          <a v-if='record.status === 0' @click='handleEdit(record)'>编辑</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.status === 0' @click='handleDelete(record.id)'>删除</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.status === 1' @click='handleUndoCheck(record)'>反审核</a>
          <a-divider v-if='record.status === 1' type='vertical' />
          <a v-if='record.status === 0' @click='handleCheck(record)'>审核</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.isClosed === 0' @click='handleClose(record)'>关闭</a>
          <a v-if='record.isClosed === 1' @click='handleUndoClose(record)'>反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="采购订单详情" key="1">
        <HfPurchaseInputDetailList
          :mainId="selectedMainId"
          :sourceSerial="selectedSerial"
          :supplierId="selectedSupplierId"
          ref="HfPurchaseinputDetailList"
        ></HfPurchaseInputDetailList>
      </a-tab-pane>
      <a-tab-pane key='2' tab='标签'>
        <HfPurchaseInputDetailLabelList ref='labelListRef' :mainId='selectedMainId' />
      </a-tab-pane>
    </a-tabs>

    <HfPurchaseInputModal ref="modalForm" @ok="modalFormOk"></HfPurchaseInputModal>
    <hf-purchase-input-hand-made-modal ref="modalForm1" @ok="modalFormOk"></hf-purchase-input-hand-made-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, postAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import HfPurchaseInputModal from '@views/purchase/modules/HfPurchaseInputModal.vue'
import HfPurchaseInputDetailList from '@views/purchase/HfPurchaseInputDetailList.vue'
import HfPurchaseInputHandMadeModal from '@views/purchase/modules/HfPurchaseInputHandMadeModal.vue'
import HfPurchaseInputDetailLabelList from '@views/purchase/HfPurchaseInputDetailLabelList.vue'

export default {
  name: 'HfPurchaseOrderList',
  mixins: [JeecgListMixin],
  components: {
    HfPurchaseInputDetailLabelList,
    HfPurchaseInputHandMadeModal,
    HfPurchaseInputDetailList,
    HfPurchaseInputModal
  },
  data() {
    return {
      description: '采购订单管理页面',
      uploadModalJD: {
        visible: false,
        loading: false
      },
      isorter: {
        column: 'documentDate',
        order: 'desc'
      },
      // 表头
      columns: [
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate'
        },
        {
          title: '供应商订单号',
          align: 'center',
          dataIndex: 'supplierOrderSerial'
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'supplierId_dictText'
        },
        {
          title: '源单号',
          align: 'center',
          dataIndex: 'sourceSerial'
        },
        {
          title: '源单类型',
          align: 'center',
          dataIndex: 'sourceType'
        },
        {
          title: '红蓝单',
          align: 'center',
          dataIndex: 'isRed_dictText'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed_dictText',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseInput/list',
        submit: '/purchase/hfPurchaseInput/submit',
        delete: '/purchase/hfPurchaseInput/delete',
        deleteBatch: '/purchase/hfPurchaseInput/deleteBatch',
        exportXlsUrl: '/purchase/hfPurchaseInput/exportXls',
        importExcelUrl: 'purchase/hfPurchaseInput/importExcel',
        importExcelUrlByJD: 'purchase/hfPurchaseInput/importExcelByJD',
        check: '/purchase/hfPurchaseInput/check',
        undoCheck: '/purchase/hfPurchaseInput/undoCheck',
        close: '/purchase/hfPurchaseInput/close',
        undoClose: '/purchase/hfPurchaseInput/undoClose',
        print: ''
      },
      dictOptions: {
        supplier: [],
        purchaseType: [],
        payType: []
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '30', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      selectedSerial: '',
      selectedSupplierId: ''
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importExcelUrlByJD: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlByJD}`
    }
  },
  methods: {
    handleDateInterval(value) {
      this.queryParam['documentDate_begin'] = value[0].format('YYYY-MM-DD')
      this.queryParam['documentDate_end'] = value[1].format('YYYY-MM-DD')
      console.log(this.endDate)
    },
    customUpload(options) {
      this.uploadModalJD.loading = true
      const { file, onProgress, onSuccess, onError } = options
      const formData = new FormData()
      formData.append('file', file)
      postAction(this.importExcelUrlByJD, formData, { headers: this.tokenHeader, timeout: 0 })
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch(err => {
          console.log('err', err)
        })
        .finally(() => {
          this.uploadModalJD.visible = false
          this.uploadModalJD.loading = false
        })
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
      this.selectedSerial = ''
      this.selectedSupplierId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows

      this.selectedSerial = selectionRows[0].serial
      this.selectedSupplierId = selectionRows[0].supplierId
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true

      if (this.beginDate) {
        params.beginDate = this.beginDate
      }
      if (this.endDate) {
        params.endDate = this.endDate
      }
      if (this.materialCode) {
        params.materialCode = this.materialCode
      }
      if (this.materialName) {
        params.materialName = this.materialName
      }

      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    }, searchReset() {
      this.queryParam = {}
      if (this.$refs.superQueryModal !== undefined) {
        this.$refs.superQueryModal.handleReset()
      }
      this.loadData(1)
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
